package com.example.myapplication.ui.activity.collect

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.ui.fragment.collect.ArticleCollectFragment
import com.example.myapplication.ui.fragment.collect.WebsiteCollectFragment
import com.kun.base.adapter.MyFragmentPagerAdapter
import com.kun.base.ui.AbsBaseActivity
import com.kun.base.ui.AbsBaseFragment
import com.kun.base.util.ToastUtils
import kotlinx.android.synthetic.main.activity_my_collection.*
import kotlinx.android.synthetic.main.activity_my_collection.magic_indicator
import kotlinx.android.synthetic.main.toolbar_layout.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 *我的收藏
 *
 * @author : Kun
 * @date : 2021/6/7
 * @time : 16:07
 */
@Route(path = ARouterPath.MY_COLLECTION)
class MyCollectionActivity : AbsBaseActivity() {
    private val titles = arrayOf("文章", "网站")

    private lateinit var adapter: MyFragmentPagerAdapter

    private lateinit var fragments: ArrayList<AbsBaseFragment>

    private lateinit var myCollectionViewModel: MyCollectionViewModel
    override fun getLayoutId(): Int {
        return R.layout.activity_my_collection
    }

    override fun initViewModel() {
        myCollectionViewModel = getActivityScopeViewModel(MyCollectionViewModel::class.java)
    }

    override fun initView() {
        fragments = ArrayList()
        fragments.add(ArticleCollectFragment())
        fragments.add(WebsiteCollectFragment())
        adapter = MyFragmentPagerAdapter(supportFragmentManager, fragments, titles.asList())

        vp_collection.adapter = adapter

        initMagicIndicator()

        iv_back.setOnClickListener { finish() }
        tv_title.text = getString(R.string.my_collection)

        iv_close.setBackgroundResource(R.drawable.add)
        iv_close.visibility = View.VISIBLE
        iv_close.setOnClickListener {
            ARouter.getInstance().build(ARouterPath.ADD_COLLECTION)
                .withInt("type", vp_collection.currentItem).navigation(this, 100)
        }
    }

    private fun initMagicIndicator() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            @RequiresApi(Build.VERSION_CODES.M)
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {

                val simplePagerTitleView = SimplePagerTitleView(context)
                simplePagerTitleView.text = titles[index]
                simplePagerTitleView.selectedColor = getColor(R.color.c_666666)
                simplePagerTitleView.normalColor = getColor(R.color.c_999999)

                simplePagerTitleView.setOnClickListener {
                    vp_collection.currentItem = index
                }

                return simplePagerTitleView
            }

            override fun getCount(): Int {
                return fragments.size
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                return LinePagerIndicator(context).apply {
                    mode = LinePagerIndicator.MODE_WRAP_CONTENT
                    setColors(Color.parseColor("#3587f2"))
                }
            }

        }
        magic_indicator.navigator = commonNavigator
        ViewPagerHelper.bind(magic_indicator, vp_collection)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            ToastUtils.showShortToast("添加收藏成功！")
            myCollectionViewModel.unCollectArticle.value = vp_collection.currentItem
        }else  if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
            ToastUtils.showShortToast("修改收藏网站成功！")
            myCollectionViewModel.unCollectArticle.value = 1
        }
    }
}